package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.text.GetChars;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.BalanceBean;
import com.magic.smartcity.beans.FeedbackBean;
import com.magic.smartcity.beans.UserBean;
import com.magic.smartcity.databinding.FragmentMeEditPasswordBinding;
import com.magic.smartcity.interfaces.IUserCallBack;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;
import com.magic.smartcity.utils.Constant;

import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeEditPasswordFragment extends BaseUserCallBackFragment {


    private FragmentMeEditPasswordBinding mBinding;
    @Inject
    IUserPresenter mPresenter;
    private String TAG = "EditPasswordFragment";
    private String mToken;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        mBinding = FragmentMeEditPasswordBinding.inflate(getLayoutInflater());
        mPresenter.registerCallBack(this);
        return mBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.user_info_edit, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.edit_user) {

            if (Constant.TOKEN != null) {
                mPresenter.updatePassword(mBinding.oldpassword.getText().toString(),mBinding.newpassword.getText().toString());
            }
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void updatePassword(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        Log.d(TAG,"用户密码更新成功!");
    }

    @Override
    public void error(String str) {
        Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
    }
}